package com.redoor.app.stake.basicdata.controller;

import com.redoor.app.stake.auth.commons.StakeApplicationStatus;
import com.redoor.app.stake.basicdata.model.BaseParkingSpace;
import com.redoor.app.stake.basicdata.model.controllerModel.BaseParkingSpaceCriteria;
import com.redoor.app.stake.basicdata.model.serviceModel.BaseParkingSpaceServiceBean;
import com.redoor.app.stake.basicdata.service.BaseParkingSpaceService;
import com.redoor.app.stake.common.model.Page;
import com.redoornetwork.framework.core.response.ResponseResult;
import com.redoornetwork.framework.core.response.ResultGenerator;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
* Created by wangjunlei on 2018-02-12 10:08:10.
*/
@RestController
@RequestMapping("/base/parking/spaces")
public class BaseParkingSpaceController {

    @Autowired
    private BaseParkingSpaceService baseParkingSpaceService;

    /**
     *  停车位更新
     *
     * @author ZhangMumu
     * @date 2018/3/22 0022 18:11
     * @param
     * @return
     */
    @ApiOperation(value = "新增或更新停车位")
    @RequiresPermissions("base:parkingSpace:insert")
    @PutMapping
    public ResponseResult updateOrsave(@RequestBody BaseParkingSpace baseParkingSpace) {
        try{
            baseParkingSpaceService.savePrakSpace(baseParkingSpace);
            return ResultGenerator.generatorSuccess();
        }catch (Exception e){
            e.printStackTrace();
            return ResultGenerator.generatorFail(StakeApplicationStatus.OTHER_EXCEPTION);
        }
    }

    /**  
     *  停车位删除
     *   
     * @author ZhangMumu  
     * @date 2018/3/22 0022 17:59  
     * @param   
     * @return   
     */  
    @ApiOperation(value = "停车位删除,传入值id='主键'")
    @RequiresPermissions("base:parkingSpace:delete")
    @PostMapping("/del")
    public ResponseResult delete(String id) {
        try{
            Integer updateNum = baseParkingSpaceService.delParkSpace(id);
            if(updateNum > 0){
                return ResultGenerator.generatorSuccess();
            }else {
                return ResultGenerator.generatorFail(StakeApplicationStatus.OTHER_EXCEPTION);
            }
        }catch (Exception e){
            e.printStackTrace();
            return ResultGenerator.generatorFail(StakeApplicationStatus.OTHER_EXCEPTION);
        }
    }
    
    /**  
     *  停车位详情
     *   
     * @author ZhangMumu  
     * @date 2018/3/22 0022 21:36
     * @param   
     * @return   
     */
    @ApiOperation(value = "详情查询,传入:id='主键'")
    @PostMapping("/detail")
    public ResponseResult detail(String id) {
        BaseParkingSpaceServiceBean baseParkingSpace = this.baseParkingSpaceService.qryParkingSpaceByUuid(id);
        return ResultGenerator.generatorSuccess(baseParkingSpace);
    }

    @ApiOperation(value = "/list", notes = "停车位信息查询列表")
    @RequiresPermissions("base:parkingSpace:list")
    @GetMapping("/list")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "limit", value = "偏移量", paramType = "query", dataType = "int", required = true, defaultValue = "20"),
            @ApiImplicitParam(name = "offset", value = "起始记录", paramType = "query", dataType = "int", required = true, defaultValue = "1"),
            @ApiImplicitParam(name = "name", value = "停车位名称", paramType = "query", dataType = "String", required = false, defaultValue = ""),
            @ApiImplicitParam(name = "code", value = "停车位编码", paramType = "query", dataType = "String", required = false, defaultValue = ""),
            @ApiImplicitParam(name = "parkingAreaUuid", value = "所属停车区uuid", paramType = "query", dataType = "String", required = false, defaultValue = ""),
            @ApiImplicitParam(name = "stationUuid", value = "所属充充电站uuid", paramType = "query", dataType = "String", required = false, defaultValue = "")
    })
    public ResponseResult list(BaseParkingSpaceCriteria criteria) {
        List<BaseParkingSpaceServiceBean> list = baseParkingSpaceService.qryParkingSpaceList(criteria);
        Integer total = baseParkingSpaceService.qryParkingSpaceCnt(criteria);
        Page pageInfo = new Page();
        pageInfo.setTotal(total);
        pageInfo.setData(list);
        return ResultGenerator.generatorSuccess(pageInfo);
    }

    /**
     * 根据传入停车区uuid获取其下所有停车位,并且标注停车位是否已经绑定枪
     *
     * @author ZhangMumu
     * @date 2018/3/22 15:15
     */
    @ApiOperation(value = "获取的停车位列表中有是否绑定枪的标记,传值:parkingAreaUuid='停车区主键'")
    @PostMapping("/listByArea")
    public ResponseResult qryParkingSpaceByStakeUuid(String parkingAreaUuid){
        List<BaseParkingSpaceServiceBean> list = this.baseParkingSpaceService.qryParkingSpaceByParkingAreaUuidOrderByBindCharger(parkingAreaUuid) ;
        return ResultGenerator.generatorSuccess(list);
    }
    /**
     * 根据停车位名称进行数据验证
     * @Author zhangYiHang
     * @Date 2018/4/3 16:38
     * @Param
     */
    @ApiOperation(value = "根据传入的停车位名称进行数目查询,返回不为0为已有此名称数据")
    @PostMapping("/qrySpaceByName")
    public ResponseResult qryParkingSpaceCountByName(String name){
        int parkingSpaceCount = this.baseParkingSpaceService.qryParkingSpaceCountByName(name);
        return ResultGenerator.generatorSuccess(parkingSpaceCount);
    };
    /**
     * 根据停车区uuid查询停车位状态
     * @Author zhangYiHang
     * @Date 2018/4/15 17:30
     * @Param
     */
    @ApiOperation(value = "根据停车区uuid查询是否绑定停车位,并且判断停车位是否绑定枪. 未绑定位:0,已绑定位未绑定枪:1,绑定位且绑定枪:2")
    @PostMapping("/qryParkingBingChanage")
    public  ResponseResult qryParkingBingChanage(String uuid){
        int status = this.baseParkingSpaceService.qryParkingBingChanage(uuid);
        return ResultGenerator.generatorSuccess(status);
    }

}
